<?php

    class Seatting_admin extends Admin_Controller {

        function Seatting_admin() {
            parent::Admin_Controller();
            $this->load->model('Seatting_admin_m');
            $this->load->library('pagination');
            //$this->load->library('upload');
            $this->load->library('session');
            $this->load->helper('date');

            $this->_data['erreur'] = "";
        }

        function index() {
            $this->list_gardient();
        }

        //BEGIN LIST GARDIENT
        function list_gardient() {
            is_admin();
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 10;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/seatting_admin/list_gardient/' . '/' . $per_page . '/';
            $config['per_page'] = '10';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Seatting_admin_m->count_gardient();
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['query'] = $this->Seatting_admin_m->list_gardient($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Seatting_admin_m->count_gardient();
            $this->_data['page_title'] = "Liste de gardient";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('admin/list_gardient');
        }

        function add_gardient() {
            is_admin();
            if ($this->input->post('submit')) {
                $formVal = array(
                array(
                'field' => 'gardient_name',
                'Label' => 'Gardient name',
                'rules' => 'required|callback_is_name_existe|xss_clean|max_length[20]'
                ),
                array(
                'field' => 'email',
                'Label' => 'Email',
                'rules' => 'trim|required|valid_email|callback_check_exist_email|xss_clean'
                ),
                array(
                'field' => 'password',
                'Label' => 'Password',
                'rules' => 'required|matches[confirm_pass]|min_length[5]'
                ),
                array(
                'field' => 'confirm_pass',
                'Label' => 'Confirm Password',
                'rules' => 'required'
                ),
                array(
                'field' => 'first_name',
                'Label' => 'First name',
                'rules' => 'required'
                ),
                array(
                'field' => 'last_name',
                'Label' => 'Last name',
                'rules' => 'required'
                ),
                array(
                'field' => 'phone',
                'Label' => 'Phone number',
                'rules' => 'required|min_length[10]'
                ),
                array(
                'field' => 'post_code',
                'Label' => 'Post code',
                'rules' => 'required'
                ),
                array(
                'field' => 'city',
                'Label' => 'City',
                'rules' => 'required'
                ),
                array(
                'field' => 'experience',
                'Label' => 'Experience number',
                'rules' => 'integer'
                ),
                array(
                'field' => 'address',
                'Label' => 'Address',
                'rules' => 'required'
                )
                );
                $this->form_validation->set_rules($formVal);

                if ($this->form_validation->run() == TRUE) {
                    $image_path = './assets/avatar_gardient';
                    $thumb_path = $image_path . '/thumbs';
                    $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' =>TRUE
                    );
                    $this->_data['page_title'] = "Uploading...";
                    $this->load->library('upload', $config);

                    if ($this->upload->do_upload()) {
                        $upload_info = $this->upload->data();
                        $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                        );
                        $this->load->library('image_lib', $config); //load library
                        $this->image_lib->resize(); //do whatever specified in config
                        delete_pro_file($upload_info['full_path']);

                        $birthday = $this->input->post('birth_year') . '-' . $this->input->post('birth_month') . '-' . $this->input->post('birth_date') . ' ' . '06:00';
                        $dob_long = human_to_unix($birthday);
                        $email = $this->input->post('email');
                        $password = $this->input->post('password');
                        $confirm_password = $this->input->post('confirm_pass');
                        $data_account = array(
                        'password' => md5($password),
                        'email' => $this->input->post('email'),
                        'gardient_name' => $this->input->post('gardient_name')
                        );
                        $this->Seatting_admin_m->add_gardient($data_account);

                        $query_id_max = $this->Seatting_admin_m->get_account_id_max();
                        $user = $query_id_max->row();
                        $user_id = $user->new_id;
                        $display_name = $this->input->post('display_name');

                        $data_profile = array('gardient_id' => $user_id,
                        'first_name' => $this->input->post('first_name'),
                        'last_name' => $this->input->post('last_name'),
                        'dob' => $dob_long,
                        'gender' => $this->input->post('gender'),
                        'phone' => $this->input->post('phone'),
                        'experience' => $this->input->post('experience'),
                        'address' => $this->input->post('address'),
                        'avatar' => $upload_info['file_name'],
                        'available' => 1,
                        'post_code'=>$this->input->post('post_code'),
                        'city'=>$this->input->post('city')
                        );
                        $this->Seatting_admin_m->add_gardient_profile($data_profile);
                        redirect('seatting_admin/list_gardient');
                    } else {
                        $birthday = $this->input->post('birth_year') . '-' . $this->input->post('birth_month') . '-' . $this->input->post('birth_date') . ' ' . '06:00';
                        $dob_long = human_to_unix($birthday);
                        $email = $this->input->post('email');
                        $password = $this->input->post('password');
                        $confirm_password = $this->input->post('confirm_pass');
                        $data_account = array(
                        'password' => md5($password),
                        'email' => $this->input->post('email'),
                        'gardient_name' => $this->input->post('gardient_name')
                        );
                        $this->Seatting_admin_m->add_gardient($data_account);

                        $query_id_max = $this->Seatting_admin_m->get_account_id_max();
                        $user = $query_id_max->row();
                        $user_id = $user->new_id;
                        $display_name = $this->input->post('display_name');

                        $data_profile = array('gardient_id' => $user_id,
                        'first_name' => $this->input->post('first_name'),
                        'last_name' => $this->input->post('last_name'),
                        'dob' => $dob_long,
                        'gender' => $this->input->post('gender'),
                        'phone' => $this->input->post('phone'),
                        'experience' => $this->input->post('experience'),
                        'address' => $this->input->post('address'),
                        'available' => 1,
                        'post_code'=>$this->input->post('post_code'),
                        'city'=>$this->input->post('city')
                        );
                        $this->Seatting_admin_m->add_gardient_profile($data_profile);

                        redirect('seatting_admin/list_gardient');
                    }
                } else {
                    $this->display_view('admin/add_gardient');
                }
            } else {
                $this->display_view('admin/add_gardient');
            }
        }

        function is_name_existe() {
            $name = $this->input->post('gardient_name');
            $query = $this->Seatting_admin_m->is_name_existe($name);
            if ($query->num_rows() > 0) {
                $this->form_validation->set_message('is_name_existe', 'The name was exist');
                return FALSE;
            } else {
                return TRUE;
            }
        }

        function check_exist_email() {
            $email = $this->input->post('email');
            $query = $this->Seatting_admin_m->get_exist_email($email);
            if ($query->num_rows() > 0) {
                $this->form_validation->set_message('check_exist_email', 'The email address was exist');
                return FALSE;
            } else {
                return TRUE;
            }
        }

        function detail_gardient($id) {
            $query = $this->Seatting_admin_m->get_detail_gardient($id);
            $this->_data['query'] = $query;
            $this->_data['page_title'] = "Profile";
            $this->display_view('admin/detail_gardient');
        }

        function set_unavailable_gardient($id) {
            is_admin();
            $this->Seatting_admin_m->set_unavailable_gardient($id);
            redirect(site_url('seatting_admin'));
        }

        function set_available_gardient($id) {
            is_admin();
            $this->Seatting_admin_m->set_available_gardient($id);
            redirect(site_url('seatting_admin'));
        }

        function delete_gardient($id, $per_page, $off_set) {
            $this->Seatting_admin_m->delete_gardient($id);
            redirect('seatting_admin/list_gardient' . '/' . $per_page . '/' . $off_set);
        }

        //END LIST GARDIENT
        //BEGIN TEXT - Tieu de, the vip ...
        function list_text() {
            is_admin();
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 7;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/seatting_admin/list_text/' . '/' . $per_page . '/';
            $config['per_page'] = '7';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->db->count_all('seating_text');
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['query'] = $this->Seatting_admin_m->list_text($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->db->count_all('seating_text');
            $this->_data['page_title'] = "Liste de gardient";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('admin/list_text');
        }

        function add_text() {
            is_admin();
            if ($this->input->post('submit')) {
                $formVal = array(
                array(
                'field' => 'title',
                'Label' => 'Title',
                'rules' => 'required|callback_is_title_existe|xss_clean'
                ),
                array(
                'field' => 'content',
                'Label' => 'Content',
                'rules' => 'required'
                )
                );
                $this->form_validation->set_rules($formVal);

                if ($this->form_validation->run() == TRUE) {
                    $image_path = './assets/text_img';
                    $thumb_path = $image_path . '/images';
                    $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' =>TRUE
                    );
                    $this->_data['page_title'] = "Uploading...";
                    $this->load->library('upload', $config);

                    if ($this->upload->do_upload()) {
                        $upload_info = $this->upload->data();
                        $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                        );
                        $this->load->library('image_lib', $config); //load library
                        $this->image_lib->resize(); //do whatever specified in config
                        //delete_pro_file($upload_info['full_path']);

                        $data = array(
                        'title' => $this->input->post('title'),
                        'content' => $this->input->post('content'),
                        'image'=>$upload_info['file_name']
                        );
                        $this->Seatting_admin_m->add_text($data);

                        redirect('seatting_admin/list_text');
                    }
                    else{
                        $data = array(
                        'title' => $this->input->post('title'),
                        'content' => $this->input->post('content')
                        );
                        $this->Seatting_admin_m->add_text($data);

                        redirect('seatting_admin/list_text');
                    }
                } else {
                    $this->display_view('admin/add_text');
                }
            } else {
                $this->display_view('admin/add_text');
            }
        }

        function edit_text($id) {
            is_admin();
            $this->_data['page_title'] = "Edit Text";
            $text = $this->Seatting_admin_m->get_detail_text($id);
            $this->_data['text'] = $text;
            if ($this->input->post('submit')) {
                $formVal = array(
                array(
                'field' => 'title',
                'Label' => 'Title',
                'rules' => 'required'
                ),
                array(
                'field' => 'content',
                'Label' => 'Content',
                'rules' => 'required'
                )
                );
                $this->form_validation->set_rules($formVal);

                if ($this->form_validation->run() == TRUE) {
                    $image_path = './assets/text_img';
                    $thumb_path = $image_path . '/images';
                    $config = array(
                    'allowed_types' => "jpg|jpeg|gif|png",
                    'upload_path' => $image_path,
                    'max_size' => 10000,
                    'encrypt_name' =>TRUE
                    );
                    $this->_data['page_title'] = "Uploading...";
                    $this->load->library('upload', $config);

                    if ($this->upload->do_upload()) {
                        $upload_info = $this->upload->data();
                        $config = array(
                        'source_image' => $upload_info['full_path'], //get original image
                        'new_image' => $thumb_path, //save as new image //need to create thumbs first
                        'maintain_ratio' => true,
                        'width' => 300,
                        'height' => 200
                        );
                        $this->load->library('image_lib', $config); //load library
                        $this->image_lib->resize(); //do whatever specified in config
                        //delete_pro_file($upload_info['full_path']);

                        $data = array(
                        'title'=>$this->input->post('title'),
                        'content' => $this->input->post('content'),
                        'image' => $upload_info['file_name']
                        );
                        $this->Seatting_admin_m->edit_text($data,$id);

                        redirect('seatting_admin/list_text');
                    }else{
                        $data = array(
                        'title'=>$this->input->post('title'),
                        'content' => $this->input->post('content')
                        );
                        $this->Seatting_admin_m->edit_text($data,$id);

                        redirect('seatting_admin/list_text');
                    }
                } else {
                    $this->display_view('admin/edit_text');
                }
            } else {
                $this->display_view('admin/edit_text');
            }
        }

        function is_title_existe() {
            $title = $this->input->post('title');
            $query = $this->Seatting_admin_m->is_title_existe($title);
            if ($query->num_rows() > 0) {
                $this->form_validation->set_message('is_title_existe', 'The name was exist');
                return FALSE;
            } else {
                return TRUE;
            }
        }

        function detail_text($id) {
            $query = $this->Seatting_admin_m->get_detail_text($id);
            $this->_data['query'] = $query;
            $this->_data['page_title'] = "TITLE";
            $this->display_view('admin/detail_title');
        }

        function delete_text($id, $per_page, $off_set) {
            $this->Seatting_admin_m->delete_text($id);
            redirect('seatting_admin/list_text' . '/' . $per_page . '/' . $off_set);
        }

        //END TEXT
        //BEGIN LIST Booking
        function list_booking() {
            is_admin();
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 7;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/seatting_admin/list_booking/' . '/' . $per_page . '/';
            $config['per_page'] = '7';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Seatting_admin_m->count_booking();
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['query'] = $this->Seatting_admin_m->get_booking($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Seatting_admin_m->count_booking();
            $this->_data['page_title'] = "Liste de booking";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('admin/list_booking');
        }

        function delete_booking($id,$per_page,$off_set) {
            $this->Seatting_admin_m->delete_booking($id);
            redirect('seatting_admin/list_booking'.'/'.$per_page.'/'.$off_set);
        }

        function detail_booking($id) {
            $query = $this->Seatting_admin_m->get_booking_by_id($id);
            $this->_data['query'] = $query;
            $this->_data['page_title'] = "Booking";
            $this->display_view('admin/detail_booking');
        }

        //END booking
        //BEGIN RECOMMANDATION
        function list_feedback() {
            is_admin();
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 7;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/seatting_admin/list_feedback/' . '/' . $per_page . '/';
            $config['per_page'] = '7';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Seatting_admin_m->count_feedback();
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['query'] = $this->Seatting_admin_m->get_feedback($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Seatting_admin_m->count_feedback();
            $this->_data['page_title'] = "Liste de recomendation";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('admin/list_feedback');
        }

        function detail_feedback($id) {
            $query = $this->Seatting_admin_m->get_feedback_by_id($id);
            $this->_data['query'] = $query;
            $this->_data['page_title'] = "RECOMMANDATION";
            $this->display_view('admin/detail_feedback');
        }

        function set_active($id) {
            is_admin();
            $this->Seatting_admin_m->set_active($id);
            redirect(site_url('seatting_admin/list_feedback'));
        }

        function set_inactive($id) {
            is_admin();
            $this->Seatting_admin_m->set_inactive($id);
            redirect(site_url('seatting_admin/list_feedback'));
        }

        function delete_feedback($id,$per_page,$off_set) {
            $this->Seatting_admin_m->delete_feedback($id);
            redirect('seatting_admin/list_feedback'.'/'.$per_page.'/'.$off_set);
        }
        //end recommandation

        //BEGIN contact
        function list_contact() {
            is_admin();
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 7;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/seatting_admin/list_contact/' . '/' . $per_page . '/';
            $config['per_page'] = '7';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Seatting_admin_m->count_contact();
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['query'] = $this->Seatting_admin_m->get_contact($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Seatting_admin_m->count_contact();
            $this->_data['page_title'] = "Liste de contact";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('admin/list_contact');
        }

        function delete_contact($id,$per_page,$off_set) {
            $this->Seatting_admin_m->delete_contact($id);
            redirect('seatting_admin/list_contact'.'/'.$per_page.'/'.$off_set);
        }

        function detail_contact($id) {
            $query = $this->Seatting_admin_m->get_contact_by_id($id);
            $this->_data['query'] = $query;
            $this->_data['page_title'] = "contact";
            $this->display_view('admin/detail_contact');
        }

        //END contact
        //begin blog
        function list_blog() {
            is_admin();
            $per_page = $this->uri->segment(3);
            if ($per_page == "") {
                $per_page = 7;
            }
            $off_set = $this->uri->segment(4);
            if ($off_set == "") {
                $off_set = 0;
            }
            $config['base_url'] = base_url() . 'index.php/seatting_admin/list_blog/' . '/' . $per_page . '/';
            $config['per_page'] = '7';
            $config['uri_segment'] = 4;
            $config['total_rows'] = $this->Seatting_admin_m->count_blog();
            if ($off_set > 0 && $off_set == $config['total_rows']) {
                $off_set = $off_set - $per_page;
            }
            $this->_data['query'] = $this->Seatting_admin_m->get_blog($config['per_page'], $off_set);
            $this->pagination->initialize($config);
            $this->_data['pagination'] = $this->pagination->create_links();
            $this->_data['total'] = $this->Seatting_admin_m->count_blog();
            $this->_data['page_title'] = "Liste de blog";
            $this->_data['per_page'] = $per_page;
            $this->_data['off_set'] = $off_set;
            $this->display_view('admin/list_blog');
        }

        function delete_blog($blog_id, $per_page, $off_set) {
            is_admin();

            $this->Seatting_admin_m->delete_blog_comment($blog_id);

            $blog_by_id = $this->Seatting_admin_m->get_blog_by_id($blog_id, $per_page, $off_set);
            if ($blog_by_id->num_rows() > 0) {
                $b = $blog_by_id->row();
                $file_name = './assets/img_blog/thumbs/' . $b->image;
                if ($b->image != null) {
                    delete_pro_file($file_name);
                }
            }

            $this->Seatting_admin_m->delete_blog($blog_id);

            redirect(site_url('seatting_admin/list_blog') . '/' . $per_page . '/' . $off_set);
        }

        //end blog
        function search_gardient() {
            if ($this->input->post('search')) {
                $name = $this->input->post('search_name');
                if ($name == "") {
                    $name = '_';
                   
                }
                if ($name != null && $name != '') {
                    $per_page = $this->uri->segment(3);
                    if ($per_page == "") {
                        $per_page = 10;
                    }
                    $off_set = $this->uri->segment(4);
                    if ($off_set == "") {
                        $off_set = 0;
                    }
                    $config['base_url'] = base_url() . 'index.php/seatting_admin/search_gardient/' . $name . '/' . $per_page . '/';
                    $config['per_page'] = $per_page;
                    $config['uri_segment'] = 4;
                    $config['total_rows'] = $this->Seatting_admin_m->count_search_gardient($name);
                    if ($off_set > 0 && $off_set == $config['total_rows']) {
                        $off_set = $off_set - $per_page;
                    }
                    $this->_data['query'] = $this->Seatting_admin_m->search_gardient($name, $config['per_page'], $off_set);
                    $this->pagination->initialize($config);
                    $this->_data['pagination'] = $this->pagination->create_links();
                    $this->_data['total'] = $this->Seatting_admin_m->count_search_gardient($name);
                    $this->_data['page_title'] = "Liste de gardient";
                    $this->_data['per_page'] = $per_page;
                    $this->_data['off_set'] = $off_set;
                    $this->_data['$name_search'] = $name;
                    $this->display_view('admin/list_gardient');
                } 
               
            }
            else{
                $this->list_gardient();
                
            } 
        }



    }

?>
